<template>
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="功能说明">
            <pre>服务器在每天早上8点50分自动开始抓取艾瑞调查的数据。
爬虫会有数据采集失败的几率（例如访问艾瑞网页超时），会造成某个平台某天 全部分类 或 部分分类 数据为零。
此时可以使用这个功能，只支持采集过去的日期。</pre>
        </el-form-item>
        <el-form-item label="选择平台" prop="platform">
            <el-radio-group v-model="form.platform">
                <template v-for="p in platforms">
                    <el-radio :label="p.id">{{p.name}}</el-radio>
                </template>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="选择日期" prop="date">
            <el-date-picker v-model="form.date"
                            :editable="false"
                            value-format="yyyy-MM-dd"
                            :picker-options="pickerOptions"/>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submit" :loading="loading">抓取</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
    export default {
        name: "iresearch_data_collection",
        props: {
            platforms: {required: true}
        },
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return moment(time).startOf('day') >= moment().startOf('day');
                    }
                },
                form: {platform: null, date: null,},
                rules: {
                    platform: [{required: true, trigger: 'blur', message: '请选择平台'}],
                    date: [{required: true, trigger: 'blur', message: '请选择日期'}],
                },
                loading: false,
            };
        },
        methods: {
            submit() {
                this.$refs.form.validate(valid => {
                    if (!valid) return;
                    this.loading = true;
                    this.$http.get('/api/iresearch/data/' + this.form.platform + '/' + this.form.date)
                        .then(res => {
                            this.loading = false;
                            this.$message.success('采集到：' + res.data.got + ' 个分类数据');
                        })
                        .catch(err => {
                            this.loading = false;
                            this.$message.error(err.response.data);
                        });
                });
            },
        },
    }
</script>

<style scoped>

</style>